/*
 * 文件名：UserBusinessImpl.java
 * 版权：Copyright 2007-2015 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： UserBusinessImpl.java
 * 修改人：yanshu
 * 修改时间：2015年12月21日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.business.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.better517na.javaKeyIdGenerator.DatabaseDefinition;
import com.better517na.javaKeyIdGenerator.TableDefinition;
import com.feichuang.chineseWoodCarving.business.IUserBusiness;
import com.feichuang.chineseWoodCarving.dao.ICompanyDao;
import com.feichuang.chineseWoodCarving.dao.IUserDao;
import com.feichuang.chineseWoodCarving.model.MCompanyVo;
import com.feichuang.chineseWoodCarving.model.MRegistryVo;
import com.feichuang.chineseWoodCarving.model.MUser;

/**
 * TODO 添加类的一句话简单描述.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Service("userBusiness")
public class UserBusinessImpl extends BaseBusinessImpl implements IUserBusiness {

    /**
     * 注入用户持久化层操作实现
     */
    @Resource(name = "userDao")
    private IUserDao userDao;

    /**
     * 企业数据操作注入
     */
    @Resource(name = "companyDao")
    private ICompanyDao companyDao;

    /**
     * {@用户登录}.
     */
    @Override
    public MUser login(String userName, String passWord, int userType) {
        return userDao.login(userName, passWord, userType);
    }

    /**
     * {@用户注册处理（启用事务）}.
     */
    @Transactional
    @Override
    public Response<?> registry(MRegistryVo registryVo) {
        MUser user = registryVo.getUserInfo();
        MUser userres = userDao.selectUserByUserNo(user.getUserNo());
        Response<MUser> res = null;
        if (userres != null) {
            res = new Response<MUser>(StatusCode.PARAMETER_ERROR, "该用户名已被注册", null);
        } else {
            try {
                user.setKeyID(keyIDFactory.NewKeyID(DatabaseDefinition.GJJPKRFinancialStatementDB, TableDefinition.GJJPKRFinancialStatementDB_KcpRelationshipAccountYYYY));
            } catch (Exception e) {
                // 因为没有机器编号表示文件这儿会抛异常 but 没关系
            }
            if (userDao.insertUser(user)) {
                res = new Response<MUser>(StatusCode.SUCCESS, "注册成功", user);
                if (registryVo.getUserType() == 1) {
                    res = insertCompanyInfo(registryVo, user);
                }
            } else {
                res = new Response<MUser>(StatusCode.ERROR, "注册失败", null);
            }
        }
        return res;
    }

    /**
     * TODO 企业信息处理.
     * 
     * @param registryVo
     *            注册信息
     * @param user
     *            用户信息
     * @return Response<MUser>处理结果
     */
    private Response<MUser> insertCompanyInfo(MRegistryVo registryVo, MUser user) {
        Response<MUser> res;
        MCompanyVo companyVo = registryVo.getCompanyInfo();
        companyVo.setUserId(user.getKeyID());
        // keyID生成组件（每个库每个表有自己独立的生成规则）,后面维护此代码的同学按照这个格式写就行。
        companyVo.setKeyId(keyIDFactory.NewKeyID(DatabaseDefinition.AutoRefundReport, TableDefinition.AdsManageSystemDB_PlatformInfo));
        if (companyDao.insertMCompanyInfo(companyVo)) {
            res = new Response<MUser>(StatusCode.SUCCESS, "注册成功", user);
        } else {
            res = new Response<MUser>(StatusCode.ERROR, "注册失败", null);
        }
        return res;
    }

}
